package dao.mapper;

import bean.Product;
import bean.User;
import dao.ProductMapper;
import services.ProductService;
import util.DBUtil;
import util.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther hewenhui
 * @see <a href="hetp://www.codingmaster.com> java </a>
 */
public class ProductMapperImpl implements ProductMapper {

    @Override
    public List<Product> getProductByindex(String index) {

        if (StringUtil.isEmpty(index)){
            return null ;//参数有误
        }
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //1，和数据库建立连接
            connection= DBUtil.getConnection();
            //2，构造失去了语句
            String sql;
            if(index.trim().equals("0")){
                sql="select * from products";
                preparedStatement=connection.prepareStatement(sql);
            }else{
                sql="select * from products where `category_id` = ?";

                preparedStatement=connection.prepareStatement(sql);
                preparedStatement.setString(1,index);
            }

            System.out.println(sql);

            //3，执行sql语句
            ResultSet resultSet = preparedStatement.executeQuery();
            //根据查询结果构造user对象
            List<Product> productList = new ArrayList<>();
            while (resultSet.next()) {
                Product product = new Product();
                // 从 resultSet 中获取每一列的数据
                int id = resultSet.getInt("id"); // 商品id
                String name = resultSet.getString("name"); // 商品名称
                int category_id = resultSet.getInt("category_id"); // 所属分类id
                String description = resultSet.getString("description"); // 商品描述
                double price = resultSet.getDouble("price"); // 商品价格
                int store_id = resultSet.getInt("store_id");  // 所属商店id
                String image_url = resultSet.getString("image_url"); // 商品图片地址

                //构建商品对象
                product.setPid(id);
                product.setName(name);
                product.setClassification(category_id);
                product.setProductDescription(description);
                product.setPrice(price);
                product.setSid(store_id);
                product.setImage_url(image_url);

                productList.add(product);
            }
            return productList;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4，关闭资源
            DBUtil.close(connection,preparedStatement,null);
        }
        return null;
    }

    @Override
    public Product getProductById(int pid) {
        if (pid<=0){
            return null ;//参数有误
        }
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //1，和数据库建立连接
            connection= DBUtil.getConnection();
            //2，构造失去了语句
            String sql;

            sql="select * from products where `id` = ?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,pid);


            //3，执行sql语句
            ResultSet resultSet = preparedStatement.executeQuery();
            //根据查询结果构造user对象
            Product product = null;
            if (resultSet.next()) {
                product = new Product();
                int id = resultSet.getInt("id"); // 商品id
                String name = resultSet.getString("name"); // 商品名称
                int category_id = resultSet.getInt("category_id"); // 所属分类id
                String description = resultSet.getString("description"); // 商品描述
                double price = resultSet.getDouble("price"); // 商品价格
                int store_id = resultSet.getInt("store_id");  // 所属商店id
                String image_url = resultSet.getString("image_url"); // 商品图片地址

                //构建商品对象
                product.setPid(id);
                product.setName(name);
                product.setClassification(category_id);
                product.setProductDescription(description);
                product.setPrice(price);
                product.setSid(store_id);
                product.setImage_url(image_url);
            }
            return product;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4，关闭资源
            DBUtil.close(connection,preparedStatement,null);
        }
        return null;

    }
}
